var bdb_version = $c.details.version;
var userAgent = navigator.userAgent.match( /chrome.+?\s/i );
$( '#version' ).text( bdb_version );
$( '#user-agent' ).text( userAgent );

$( '#savebutton' ).click( Config.store );
$( '#cancelbutton' ).click( Config.restore );


// Check optional permissions.
permissionBind( $( '#ipLookup' ), { origins: IPSearchAPIOrigins } );
permissionBind( $( '#refControl' ), { origins: refOrigins, permissions: refPermissions } );
function permissionBind( checkbox, permObj ) {
	'use strict';
	$c.hasPerm( permObj, function( result ) {
		if ( !result ) {
			checkbox.prop( 'checked', false );
			checkbox.parent().click( requestRefPermission );
		}
	});

	function requestRefPermission(e) {
		'use strict';
		if( !checkbox.prop( 'checked' ) )
			return;

		$c.requestPerm( permObj, function( granted ) {
			if ( !granted ) {
				checkbox.prop( 'checked', false );
			} else {
				checkbox.parent()[0].removeEventListener( 'click', requestRefPermission );
			}
		});
	}
}


// Bind between configs.
bindInit();
addBindListener();
$( '#cancelbutton' ).click( bindInit );
function bindInit() {
	var bindingOptions = $( '[bindto]' );
	bindingOptions.forEach( function( bindingOption ){
		'use strict';
		bindingOption = $( bindingOption );

		if( !bindingOption.attr( 'bindto') )
			return;
		var bindedOption = $( '#' + bindingOption.attr( 'bindto' ) );
		if ( bindedOption.prop( 'checked' ) === false )
			bindingOption.attr( 'disabled', '' );
		else
			bindingOption.removeAttr( 'disabled' );
	});
}
function addBindListener() {
	var bindingOptions = $( '[bindto]' );
	bindingOptions.forEach( function( bindingOption ) {
		'use strict';
		bindingOption = $( bindingOption );

		if( !bindingOption.attr( 'bindto' ) )
			return;
		var bindedOption = $( '#' + bindingOption.attr( 'bindto' ) );
		bindedOption.click( function(e) {
			if (this.checked === false)
				bindingOption.attr( 'disabled', '' );
			else
				bindingOption.removeAttr( 'disabled' );
		} );
	});
}


// HotKey: Ctrl + S to save; F1 opens wiki.
$( document ).keydown( function( e ) {
	var code = e.keyCode,
		KeyS = 83,
		KeyF0 = 111;
	switch ( code ) {
		case KeyS:
			if ( e.ctrlKey ) {
				e.preventDefault();
				e.stopPropagation();
			}
			break;
		case KeyF0 + 1:
			e.preventDefault(); e.stopPropagation();
			window.open('https://code.google.com/p/xiaody/wiki/BaiDuBeautifier');
			break;
	}
} );
$( document ).keyup( function( e ) {
	var code = e.keyCode,
		KeyS = 83,
		KeyBACKSLASH = 220;
	if ( code === KeyS && e.ctrlKey ) {
		e.preventDefault();
		e.stopPropagation();
		Config.store();
	} else if ( code === KeyBACKSLASH ) {
		var _searchBox = searchBox[0];
		if ( document.activeElement === document.body )
			_searchBox.focus();
		else if ( _searchBox === document.activeElement && e.ctrlKey )
			_searchBox.select();
	}
} );


// Sync user's config change on search page to normal sections.
$( '#searchPage' ).click( onSearchPageClick );
$( '#searchPage' ).input( onSearchPageInput );
function onSearchPageClick( e ) {
	var tar = e.target;
	if ( tar.tagName === 'INPUT' && tar.type === 'checkbox' ) {
		$( '#' + tar.id ).prop( 'checked', tar.checked );
	} else if ( tar.tagName === 'SELECT' ) {
		$( '#' + tar.id ).val( tar.value );
	}
}
function onSearchPageInput( e ) {
	var tar = e.target;
	if ( tar.tagName === 'TEXTAREA' ) {
		$( '#' + tar.id ).val( tar.value );
	}
}

